<template>
  <div class="table-main">
    <el-dialog
      @close="handMyClose"
      :append-to-body="true"
      title="员工"
      :visible.sync="dialogVisible1"
      width="60%"
      :close-on-click-modal="false"
    >
      <div style="margin-top:50px;">
        <el-form ref="form" :model="form" label-width="120px" :rules="rules">
          <el-row :gutter="20" justify="center" type="flex">
            <el-col :span="16">
              <el-form-item label="角色名字:" prop="roleName">
                <el-input :disabled="isShow==3" v-model="form.roleName" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- <el-row :gutter="20" justify="center" type="flex">
            <el-col :span="16">
              <el-form-item label="商家店铺:" prop="shopId">
                <el-select
                  @change="handIds"
                  filterable
                  multiple
                  :disabled="isShow==3"
                  v-model="form.shopId"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in findAllsList"
                    multiple
                    :key="item.id"
                    :label="item.shopName"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>-->
          <el-row :gutter="20" justify="center" type="flex">
            <el-col :span="16">
              <el-form-item label="角色描述:" prop="details">
                <el-input :disabled="isShow==3" v-model="form.details" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20" justify="center" type="flex">
            <el-col :span="16">
              <el-form-item label="角色编码:" prop="roleValue">
                <el-input :disabled="isShow==3" v-model="form.roleValue" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button
          type="primary"
          @click="handFromSub('form')"
          :loading="bntLoading"
          v-if="isShow!=3"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { imgSizi } from '@/utils/myUtils'
import myPinyin from '@/utils/pinyin'
export default {
  name: 'SidebarLogo',
  components: {},
  props: {
    isState: {
      type: Number,
      default: 0
    }
  },
  watch: {
    'form.roleName'(newVal, oldVal) {
      // console.log(myPinyin.chineseToPinYin(newVal))
      if (newVal) {
        this.form.roleValue = myPinyin.chineseToPinYin(newVal)
      }
    },
    isState(newObj) {
      this.isShow = newObj
    }
  },
  data() {
    return {
      findAllsList: [],
      options: [],
      rules: {
        roleName: [
          {
            required: true,
            message: '请输入角色名称',
            trigger: 'blur'
          },
          {
            validator(rule, value, callback) {
              var reg = /^[\u0391-\uFFE5A-Za-z]+$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('角色名必须为汉字或英文字母'))
              }
            },
            trigger: 'blur'
          }
        ],
        details: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ],
        shopId: [{ required: true, message: '请选择店铺', trigger: 'change' }],
        roleValue: [
          { required: true, message: '请输入角色编码', trigger: 'blur' }
        ]
      },

      bntLoading: false,
      loading2: false,
      headers: {
        token: localStorage.getItem('mytoken') || ''
      },
      bannerType: [
        {
          label: '汽车轮播图',
          value: 2
        },
        {
          label: '商品轮播图',
          value: 1
        },
        {
          label: '首页弹窗广告',
          value: 3
        }
      ],
      form: {},

      shopStatusOptions: [],
      isShow: 0,
      imageUrl: '',
      dialogImageUrl: '',
      dialogVisible1: false,
      addressKeyword: '',
      activeName: 'first',
      dialogVisible: false,
      tipShow: false
    }
  },
  async mounted() {
    // this.findAllsList = (await this.$api.getSysFindAlls()).data.data
    console.log(myPinyin.chineseToPinYin('你好aaa'), '我')
    this.$api.selectAllRole().then(res => {
      console.log(res, '角色')
      this.options = res.data.data
    })
  },
  methods: {
    handIds(val) {
      console.log(val, 'val')
    },
    uploadChange() {},
    onError() {},
    handGetShop(id) {
      this.$api.selectRoleByIdRole({ id }).then(res => {
        console.log('查看详情', res)
        this.form = res.data.data
      })
    },
    handSuccess(res, file) {
      var url = JSON.parse(res.message)
      this.$set(this.form, 'resPath', url.url)
      this.loading2 = false
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      const isJPG = file.type === 'image/jpeg'
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      const isSize = imgSizi(file, 672, 180)

      return isSize
    },
    handMyClose() {
      for (let key in this.form) {
        this.form[key] = ''
      }
    },
    handFromSub(formName) {
      //1编辑
      if (this.isState == 1) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            this.bntLoading = true

            this.$api
              .editRole(this.form)
              .then(res => {
                this.dialogVisible1 = false
                this.$parent.init()
                console.log(res, '编辑', this.form)
                this.$message({
                  message: res.data.message,
                  type: 'success'
                })
                this.bntLoading = false
              })
              .finally(() => {
                this.bntLoading = false
              })
          } else {
            console.log('error submit!!')
            this.$message({
              message: '请完成表单录入'
            })
            this.bntLoading = false
            return false
          }
        })
      } else if (this.isState == 0) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            this.bntLoading = true
            this.$api
              .addRole(this.form)
              .then(res => {
                this.dialogVisible1 = false
                this.bntLoading = false
                this.$parent.init()
                this.$refs.form.resetFields()
                console.log(res, '添加', this.form)
                this.$message({
                  message: res.data.message,
                  type: 'success'
                })
              })
              .finally(() => {
                this.bntLoading = false
              })
          } else {
            console.log('error submit!!')
            this.$message({
              message: '请完成表单录入'
            })
            return false
          }
        })
      }
    },

    // handSuccess(response, file, fileList) {
    //   console.log(response, file, fileList, '成功回调')
    //   this.form.shopImg = response.message
    // },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    // handlePictureCardPreview(file) {
    //   this.dialogImageUrl = file.url
    //   this.dialogVisible2 = true
    // },
    handleClose() {
      this.dialogVisible = false
    },
    handMapShow() {
      this.dialogVisible = true
    },
    handleClick() {}
  }
}
</script>

<style lang="scss" scoped>
.map_btn {
  width: 82px;
  height: 22px;
  background: rgba(56, 173, 147, 1);
  border-radius: 4px;
  color: #fff;
  font-size: 14px;
  line-height: 22px;
  text-align: center;
  margin-top: 8px;
}
/deep/ .el-form-item__label {
  font-size: 14px;
  color: #000;
  font-weight: bold;
}
.form_box {
  width: 80%;
  margin: 0 auto;
}
.avatar-uploader /deep/ .el-upload {
  border: 1px dashed #38ad93;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #38ad93;
  width: 336px;
  height: 90px;
  line-height: 90px;
  text-align: center;
}
.avatar {
  width: 336px;
  height: 90px;
  display: block;
}
</style>
